<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <script src="./dist/qrcode.js"></script>
  <script src="./dist/html2canvas.js"></script>
  <script src="./dist/jspdf.js"></script>
  <title>作业证书</title>
  <style>
    .bg {
      position: relative;
      width: 1080px;
      height: 900px;
      background: url(./dist/work.png) center / 100% no-repeat;
      margin: 0 auto;
      font-family: "Arial", sans-serif;
      background-color: #fff;
      margin-top: -60px;
    }

    .absolute {
      position: absolute;
    }

    .scale {
      transform: scale(0.78);
    }

    .code {
      top: 238px;
      left: 50%;
      transform: translate(-50%);
      font-size: 22px;
      color: #151716;
      margin-left: 56px;
    }

    .ID-box {
      top: 335px;
      left: 50%;
      transform: translate(-50%);
      margin-left: -174px;
      width: 298px;
      height: 190px;
      font-size: 12px;
    }

    .user-img {
      width: 62px;
      height: 88px;
      background: center / 100% no-repeat;
      left: 21px;
      top: 20px;
    }

    .certificate-number {
      top: 27px;
      left: 98px;
    }

    .user-name {
      top: 51px;
      left: 95px;
    }

    .user-type {
      top: 51px;
      left: 184px;
    }

    .user-gender {
      top: 83px;
      left: 95px;
    }

    .user-project {
      top: 83px;
      left: 182px;
    }

    .init-date {
      top: 133px;
      left: 11px;
    }

    .effective-date {
      top: 131px;
      left: 76px;
    }

    .review-date {
      top: 155px;
      left: 10px;
    }

    .remark {
      top: 552px;
      left: 50%;
      transform: translate(-50%);
      color: #cc3032;
      font-size: 19px;
      font-weight: bold;
    }

    .qr {
      position: absolute;
      left: 76%;
      top: 63.2%;
      width: 49px;
      height: 51px;
      background: center / 100% no-repeat;
      z-index: 9999;
    }

    #exportButton {
      margin: 10px;
      padding: 8px 16px;
      background-color: #007bff;
      color: #fff;
      border: none;
      border-radius: 4px;
      cursor: pointer;
      position: absolute;
      top: 24px;
      right: 24px;
    }

    #exportButton:hover {
      background-color: #0056b3;
    }
  </style>
</head>

<body>
  <div class="bg" id="content">
    <!-- 档案编码 -->
    <div class="absolute code"></div>
    <!-- 身份证内容 -->
    <div class="ID-box absolute">
      <div class="qr" id="qrcode"></div>
      <img class="user-img absolute" id="myImage" src="">
      <!-- 证号 -->
      <div class="certificate-number absolute"></div>
      <!-- 姓名 -->
      <div class="user-name absolute scale"></div>
      <!-- 作业类别 -->
      <div class="user-type absolute scale"></div>
      <!-- 性别 -->
      <div class="user-gender absolute scale"></div>
      <!-- 操作项目 -->
      <div class="user-project absolute scale"></div>
      <!-- 初始日期 -->
      <div class="init-date absolute scale"></div>
      <!-- 有效日期 -->
      <div class="effective-date absolute scale"></div>
      <!-- 应复审日期 -->
      <div class="review-date absolute scale"></div>

    </div>
    <!-- 备注 -->
    <div class="absolute remark"></div>
  </div>
  <!-- Export button -->
  <button id="exportButton">导出</button>
  <!-- Scripts -->
  <script>

  </script>
  <script>
    var queryString = window.location.search
    var urlParams = new URLSearchParams(queryString)
    var id = urlParams.get('id')
    var info = {
      fileCode: "A53010053321001660", // 档案编码
      certificateNumber: "T532101198503213213", // 证号
      name: "赵迟", // 姓名
      workCategory: "电工作业", // 作业类别
      gender: "男", // 性别
      userProject: "低压电工作业", // 操作项目
      initialIssueDate: "2017-10-30", // 初始日期
      validityPeriod: "2021-01-13至2027-01-12", // 有效日期
      avatar: "", // 用户头像
      nextReviewDate: "2024-01-12前", // 应复审日期
      remark: "备注：本证书应于2024-01-12前进行复审" // 备注
    }
    fetch('http://api.ynjov.com:8080/api/getWorkCertificatesDetail/' + id)
      .then(response => {
        
        if (!response.ok) {
          throw new Error('Network response was not ok')
        }
        return response.json()
      })
      .then(({ data }) => {
        
        info = data
        info.avatar = data.avatar ? data.avatar : './dist/heade_test.png'
        fillContent()
      })
      .catch(error => {
        console.error('Fetch error:', error)
      })
    // 用于动态填充内容的函数
    function fillContent() {
      document.querySelector('.code').innerText = info.fileCode
      document.querySelector('.certificate-number').innerText = info.certificateNumber
      document.querySelector('.user-name').innerText = info.name
      document.querySelector('.user-type').innerText = info.workCategory
      document.querySelector('.user-gender').innerText = info.gender
      document.querySelector('.user-project').innerText = info.operationProject
      document.querySelector('.init-date').innerText = info.initialIssueDate
      document.querySelector('.effective-date').innerText = info.validityPeriod
      document.querySelector('.review-date').innerText = info.nextReviewDate
      document.querySelector('.remark').innerText = `备注：本证书应于${info.nextReviewDate}前进行复审`// 设置用户头像
      // 设置用户头像
      var userImgElement = document.querySelector('.user-img')
      // 用 info 对象中的图片 URL 替换原来的图片链接
      userImgElement.src = `http://api.ynjov.com:8080/${info?.avatar}?t=${Date.now()}`
      userImgElement.setAttribute("crossOrigin", 'Anonymous')
      // 获取当前页面的 URL
      var currentURL = `http://sys.ynjov.com/workres.html?id=${id}` // 二维码

      // 创建 QRCode 实例并将二维码添加到页面
      var qrcode = new QRCode(document.getElementById("qrcode"), {
        text: currentURL,
        width: 58,
        height: 58
      })
    }
    // 获取要导出的HTML元素
    const element = document.getElementById('content')

    const pdf = new jsPDF()
    // 使用 html2pdf 将 HTML 元素导出为 PDF
    exportButton.addEventListener('click', () => {
      document.getElementById("exportButton").hidden = true
      window.document.title = `${name}作业证书`
      window.print()
      document.getElementById("exportButton").hidden = false
      window.document.title = `作业证书`
    })
    // 页面加载时调用填充内容的函数
    // window.onload = fillContent;
  </script>
</body>

</html>